﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 客户端
{
    public partial class 购物车 : Form
    {
        string constr = login.constr;
        public static string account;
        DataSet myst = new DataSet();
        SqlDataAdapter myda;
        string gid;
        string Coid;
        string amount;
        string price;

        public 购物车()
        {
            InitializeComponent();
            sx();
        }
        void sx()
        {
            SqlConnection sqlcon = new SqlConnection(constr);
            try
            {
            sqlcon.Open();
            string sql = "select Coid as 购物车编号,Goods.gid as 商品ID,gname as 商品名称,gtype as 商品类型,gprice as 商品单价,Coamount as 预购数量,sname as 品牌, Codate as 加入时间 from Goods,Corder,Supplier where Goods.gid=Supplier.gid and Corder.gid=Goods.gid and Caccount='" + account + "'and Costate='" + "未支付" + "'";
            SqlCommand sqlcmd = new SqlCommand(sql, sqlcon);
            myda = new SqlDataAdapter(sqlcmd);
            myst.Tables.Clear();
            myda.Fill(myst, "B");
            dataGridView1.DataSource = myst.Tables["B"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "查询失败");
            }
            finally
            {
                sqlcon.Close();
            }

        }
        private void btn_buy_Click(object sender, EventArgs e)
        {
            pay.num = amount;
            pay.price = int.Parse(price);
            pay.gid = gid;
            if (amount=="")
            {
                MessageBox.Show("请选择要购买的商品");
                return;
            }
            else
            {
                pay pay = new pay();
                this.Close();
                pay.Show();
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (amount == "")
            {
                MessageBox.Show("请选择要删除的商品");
                return;
            }
            else
            {
                SqlConnection sqlcon = new SqlConnection(constr);
                try
                {
                    sqlcon.Open();
                    string sql = "delete from Corder where Coid='"+Coid+"'";
                    SqlCommand sqlcmd = new SqlCommand(sql,sqlcon);
                    int flag = sqlcmd.ExecuteNonQuery();
                    if (flag == 1)
                    {
                        MessageBox.Show("删除成功！","提示");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString()+"删除失败");
                }
                finally
                {
                    sqlcon.Close();
                    sx();
                }

            }
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dgvRow = dataGridView1.Rows[e.RowIndex];
            DataGridViewCellCollection dgvCC = dgvRow.Cells;
            Coid=dgvCC[0].Value.ToString();
            gid= dgvCC[1].Value.ToString();
            price= dgvCC[4].Value.ToString();
            amount =dgvCC[5].Value.ToString();
        }
    }
}
